package ar.camera.surfaceview;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.MotionEvent;
import android.view.SurfaceView;
import ar.camera.glsurfaceview.base.BaseGLSurfaceView;
import ar.camera.renderer.extend.LocationIconRenderer;
import ar.globals.Globals;

public class TochSurfaceView extends SurfaceView {
	private BaseGLSurfaceView mIcView;
	private long touchTime;
	private long touchDuration;

	public void setmLocationIconView(BaseGLSurfaceView mLocationIconView) {
		this.mIcView = mLocationIconView;
	}

	public TochSurfaceView(Context context) {
		super(context);
		getHolder().setFormat(PixelFormat.TRANSPARENT);
		Globals.mode = Globals.NONE;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		if (Globals.mode == Globals.LONG_PRESS) {
			onLongTouch(event);			
		} 

		switch (event.getActionMasked()) {
		case MotionEvent.ACTION_DOWN:
			touchTime = System.currentTimeMillis();
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			if (event.getPointerCount() == 3) {
				Globals.mode = Globals.SHORT_PRESS;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if (Globals.mode != Globals.LONG_PRESS) {
				touchDuration = System.currentTimeMillis() - touchTime;
				if (touchDuration > 500) {
					((LocationIconRenderer) mIcView.getmRenderer())
							.setUpdateSensor(false);
					Globals.vibrator.vibrate(100);
					Globals.mode = Globals.LONG_PRESS;
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			onShortTouch(event);
			break;
		default:
			break;
		}

		return true;
	}

	public void onShortTouch(MotionEvent event) {
		LocationIconRenderer icRenderer = (LocationIconRenderer) mIcView
				.getmRenderer();		
		icRenderer.touchScreen(event);
	}

	public void onLongTouch(MotionEvent event) {
		mIcView.onTouchEvent(event);
	}
}
